package benny.ApkposStore.Service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import benny.ApkposStore.R;
import benny.ApkposStore.utils.ApkConstant;
import benny.ApkposStore.utils.HttpDownloader;

public class DownloadService extends Service {
	private static int NOTIFICATIONS_ID = R.layout.main;
	private NotificationManager mNotificationManager;

	// private String resultString = null;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	//	Floor floor = (Floor) intent.getSerializableExtra("floor");

		DownloadThread downloadThread = new DownloadThread();
		Thread thread = new Thread(downloadThread);
		thread.start();
		return super.onStartCommand(intent, flags, startId);
	}

	public void BennyNOTIFICATIONS(String tickerText, String title,
			String content, int drawable, Class<?> mainclass) {

		Notification notification = new Notification(drawable, tickerText,
				System.currentTimeMillis());

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, mainclass), 0);

		notification.setLatestEventInfo(this, title, content, contentIntent);

		mNotificationManager.notify(NOTIFICATIONS_ID, notification);
	}

	class DownloadThread implements Runnable {
    	private int result ;
    	private String resultString  ;
		public DownloadThread() {
			
		}

		@Override
		public void run() {

			Log.i("log", "run");
			Looper.prepare();
			String urlsStr = ApkConstant.BaseUrl ;
			HttpDownloader hDownloader = new HttpDownloader();
			
			 result = hDownloader.downFile(urlsStr, "Date/",
					"shop_config.db");
			
			//+ "floor.xml"	 
//			 urlsStr = Apkconstant.BaseUrl + "room.xml";
//
//			 result = hDownloader.downFile(urlsStr, "Date/",
//					"room.xml");
//			 
//			 urlsStr = Apkconstant.BaseUrl + "turn.xml";
//
//			 result = hDownloader.downFile(urlsStr, "Date/",
//					"turn.xml");
			 resultString = null;

			 if (result == -1) {
					resultString = "下载失败！";
				} else if (result == 0) {
					resultString = "下载成功！";
				} else if (result == 1) {
					resultString = "该文件已经存在！";
				}

			Toast.makeText(getApplicationContext(), resultString,
					Toast.LENGTH_SHORT).show(); // 进入loop中的循环，查看消息队列
			Intent intent = new Intent();
			intent.setAction(ApkConstant.DOWN_MESSAGE_ACTION);
			if (result == -1) {
				intent.putExtra("msgStr", "同步失败");	
			}
			else {
				intent.putExtra("msgStr", "同步成功");	
			}
			
			sendBroadcast(intent);

			Looper.loop();
		
		}

	}
}
